# _*_ coding utf-8 _*_
# FILENAME：SelectVersionComponent.py
# DESCRIPTION: 版本选择控件,基于QComboBox控件，默认选择最近的三个版本数据
# AUTH: 
# DATE: 2022/6/59:47 上午

from PyQt5.QtWidgets import *
from sqlalchemy import desc

from components.BaseComboBox import BaseComBoBox
from models.baseModel import get_session
from models.factoryTransportTimeModel import FactoryTransportTimeModel
from models.versionModel import VersionModel


class SelectFactoryTransportTimeComponent(BaseComBoBox):

    def __init__(self, parent=None):
        super(SelectFactoryTransportTimeComponent, self).__init__()
        # print("init")
        data = self.init_data()
        arr = []
        for i in range(len(data)):
            model = data[i]
            val = model.version_name + '|' + str(model.id)
            arr.append(val)
        self.addItems(arr)

    def init_data(self):
        # print("init data")
        session = get_session()
        data = session.query(FactoryTransportTimeModel).filter(FactoryTransportTimeModel.is_del == 0).order_by(desc(FactoryTransportTimeModel.update_date)) \
            .limit(3).offset(0).all()
        session.close()
        return data

